/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.market.web.controller;

import com.market.web.EJB.CustomerFacade;
import com.market.web.model.Customer;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Mai
 */

@WebServlet("/login")
public class logInController extends HttpServlet  {

    @EJB
    private CustomerFacade customerFacade;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String email = req.getParameter("email");
            String password = req.getParameter("password");
            HttpSession userSession = req.getSession(false);
            JsonObjectBuilder obj = Json.createObjectBuilder();
            try{
                
                Customer c;
                
                c = customerFacade.getCustomerByEmail(email);
                if(c.getPassword().equals(password))
                {
                    if(c.getName().equals("Admin"))
                    {
                        userSession.setAttribute("cust", c);
                        obj.add("result", "Admin");
                        resp.setStatus(HttpServletResponse.SC_OK);
                        resp.setContentType("application/json");
                        resp.setHeader("Phrase", "I rock!");
                        //System.out.println(getServletContext().getAttribute("CurrentCust"));
                        try (PrintWriter pw = resp.getWriter()) {
                        pw.println(obj.build().toString());
//                        RequestDispatcher rd = req.getRequestDispatcher("/chat.html");
//                        rd.forward(req, resp); 
//                        return;
                    }
                    }
                    else
                    {
                        userSession.setAttribute("cust", c);
                        obj.add("result", "User");
                        resp.setStatus(HttpServletResponse.SC_OK);
                        resp.setContentType("application/json");
                        resp.setHeader("Phrase", "I rock!");
                        //System.out.println(getServletContext().getAttribute("CurrentCust"));
                        try (PrintWriter pw = resp.getWriter()) {
                        pw.println(obj.build().toString());
//                        RequestDispatcher rd = req.getRequestDispatcher("/productList.html");
//                        rd.forward(req, resp); 
//                        return;
                        }
                    }
                }
                    
            }catch(Exception ex)
            {
               
                obj.add("result", "error");
                resp.setStatus(HttpServletResponse.SC_OK);
                resp.setContentType("application/json");
                resp.setHeader("Phrase", "I rock!");
                //System.out.println(getServletContext().getAttribute("CurrentCust"));
                try (PrintWriter pw = resp.getWriter()) {
                pw.println(obj.build().toString());
            } 
//                RequestDispatcher rd = req.getRequestDispatcher("/homepage.html");
//                        rd.include(req, resp);
//                        return;
            }
    }
        
    
//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//    
//            String email = req.getParameter("email");
//            String password = req.getParameter("password");
//            HttpSession userSession = req.getSession();
//            
//            try{
//                
//                Customer c;
//                
//                c = customerFacade.getCustomerByEmail(email);
//                if(c.getPassword().equals(password))
//                {
//                    if(c.getName().equals("Admin"))
//                    {
//                        userSession.setAttribute("cust", c);
//                        RequestDispatcher rd = req.getRequestDispatcher("/chat.html");
//                        rd.forward(req, resp); 
//                        return;
//                    }
//                    else
//                    {
//                        userSession.setAttribute("cust", c);
//                        RequestDispatcher rd = req.getRequestDispatcher("/productList.html");
//                        rd.forward(req, resp); 
//                        return;
//                    }
//                    
//                }
//
//            }catch(Exception ex)
//            {
//                JsonObjectBuilder obj = Json.createObjectBuilder();
//                obj.add("result", "error");
//                resp.setStatus(HttpServletResponse.SC_OK);
//                resp.setContentType("application/json");
//                resp.setHeader("Phrase", "I rock!");
//                //System.out.println(getServletContext().getAttribute("CurrentCust"));
//                try (PrintWriter pw = resp.getWriter()) {
//                pw.println(obj.build().toString());
//            } 
////                RequestDispatcher rd = req.getRequestDispatcher("/homepage.html");
////                        rd.include(req, resp);
////                        return;
//            }
//     	}
}   
        
        
    

